﻿/****************************** Module Header ******************************\
 * Module Name:  ExMethodForRepeater.cs
 * Module Version:  1.0.0 (2011-9-30)
 * Copyright (c) AnyEB.Com (14507247@qq.com)
 * 
 * This class is used to extension webcontrol instance's method.  It supplies 
 * public static extension methods to load data or get othor control.
 * 
 * This source is subject to the GPL License.
 * See http://www.gnu.org/licenses/gpl.html.
 * All other rights reserved.
 * 
 * THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
 * EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED 
 * WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
\***************************************************************************/

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web.UI.WebControls;

namespace UCF.Core.Web
{
    /// <summary>
    /// Repeater数据控件的扩展方法
    /// </summary>
    public static class ExMethodForRepeater
    {
        public static IList<string> GetCheckedValues(this Repeater rpt,string checkboxID="chk",string hiddenID="hid")
        {
            IList<string> list = new List<string>();
            foreach (RepeaterItem ri in rpt.Items)
            {
                if (ri.ItemType != ListItemType.Item && ri.ItemType != ListItemType.AlternatingItem) continue;
                CheckBox chk = (CheckBox)ri.FindControl(checkboxID);
                if (chk.Checked)
                {
                    HiddenField hid = (HiddenField)ri.FindControl(hiddenID);
                    list.Add(hid.Value);
                }
            }
            return list;
        }
        public static IList<string> GetAllCheckedValues(this Repeater rpt, string checkboxID = "chk", string hiddenID = "hid")
        {
            IList<string> list = rpt.GetCheckedValues(checkboxID, hiddenID);
            foreach (RepeaterItem ri in rpt.Items)
            {
                if (ri.ItemType != ListItemType.Item && ri.ItemType!=ListItemType.AlternatingItem) continue;
                Repeater rpt2 = (Repeater)ri.FindControl(rpt.ID);
                if (rpt2 == null) continue;
                list.AddRangeR(rpt2.GetCheckedValues(checkboxID, hiddenID).ToArray());
                foreach (RepeaterItem ri2 in rpt2.Items)
                {
                    if (ri2.ItemType != ListItemType.Item && ri2.ItemType != ListItemType.AlternatingItem) continue;
                    Repeater rpt3 = (Repeater)ri2.FindControl(rpt.ID);
                    if (rpt3 == null) continue;
                    list.AddRangeR(rpt3.GetCheckedValues(checkboxID, hiddenID).ToArray());
                    foreach (RepeaterItem ri3 in rpt3.Items)
                    {
                        if (ri3.ItemType != ListItemType.Item && ri3.ItemType != ListItemType.AlternatingItem) continue;
                        Repeater rpt4 = (Repeater)ri3.FindControl(rpt.ID);
                        if (rpt4 == null) continue;
                        list.AddRangeR(rpt4.GetCheckedValues(checkboxID, hiddenID).ToArray());
                        foreach (RepeaterItem ri4 in rpt4.Items)
                        {
                            if (ri4.ItemType != ListItemType.Item && ri4.ItemType != ListItemType.AlternatingItem) continue;
                            Repeater rpt5 = (Repeater)ri4.FindControl(rpt.ID);
                            if (rpt5 == null) continue;
                            list.AddRangeR(rpt5.GetCheckedValues(checkboxID, hiddenID).ToArray());
                            foreach (RepeaterItem ri5 in rpt5.Items)
                            {
                                if (ri5.ItemType != ListItemType.Item && ri5.ItemType != ListItemType.AlternatingItem) continue;
                                Repeater rpt6 = (Repeater)ri5.FindControl(rpt.ID);
                                if (rpt6 == null) continue;
                                list.AddRangeR(rpt6.GetCheckedValues(checkboxID, hiddenID).ToArray());
                            }
                        }
                    }
                }
            }
            return list;
        }
        /// <summary>
        /// Repeater控件绑定DataTable数据源
        /// </summary>
        /// <param name="rplControl"></param>
        /// <param name="dataTable"></param>
        public static void LoadData(this Repeater rplControl, ref DataTable dataTable)
        {
            rplControl.DataSource = dataTable;
            rplControl.DataBind();
        }
        /// <summary>        /// 
        /// Repeater控件绑定IList泛型数据源
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="rplControl"></param>
        /// <param name="iList"></param>
        public static void LoadData<T>(this Repeater rplControl, IEnumerable<T> iList)
        {
            rplControl.DataSource = iList;
            rplControl.DataBind();
        }
        /// <summary>
        /// Repeater绑定数据源
        /// </summary>
        /// <param name="rplControl"></param>
        /// <param name="Idr"></param>
        public static void LoadData(this Repeater rplControl, ref IDataReader Idr)
        {
            rplControl.DataSource = Idr;
            rplControl.DataBind();
        }
    }
}
